﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace MVCTippSpielManagement.Management
{
	public class LogOnUser : User
	{

		// Class variable
		protected bool _isLogOnUserinDB;


		#region Konstruktor
		public LogOnUser()
		{
			//Default Konstruktor
			UserManagement _UserManagement = new UserManagement();
		}

		public LogOnUser(string _Username)
		{
			//Konstruktor -> Parameter Username

			// Create object UserManagement
			_UserManagement = new UserManagement();

			this.Username = _Username;
			_isLogOnUserinDB = false;

			// Read user info
			ReadUserInformations();
			if (_isLogOnUserinDB == true)
			{
				ReadUserAdresses();
				ReadUserGroups();
			}

		}

		#endregion


		protected override void ReadUserInformations()
		{

			try
			{
				SqlParameter _Parameter = new SqlParameter("@cUsername", _Username);
				DataTable dt = MVCTippSpielManagement.Management.DataBaseManager.ExecuteDataTable("GetLogOnUser", _Parameter);

				this.ID = System.Convert.ToInt32(dt.Rows[0]["ID"].ToString().Trim());
				this.Username = dt.Rows[0]["Username"].ToString().Trim();
				this.Firstname = dt.Rows[0]["Vorname"].ToString().Trim();
				this.Surname = dt.Rows[0]["Nachname"].ToString().Trim();
				this.BirthDate = System.Convert.ToDateTime(dt.Rows[0]["Geburtsdatum"].ToString().Trim());
				this.EMail = dt.Rows[0]["Email"].ToString().Trim();
				this.Password = dt.Rows[0]["Passwort"].ToString().Trim();
				this.SecretQuestionID = System.Convert.ToInt32(dt.Rows[0]["FK_Geheimfrage"].ToString().Trim());
				this.SecretQuestionAnswer = dt.Rows[0]["Antwort"].ToString().Trim();
				this.State = dt.Rows[0]["FK_Status"].ToString().Trim();
				_isLogOnUserinDB = true;

			}
			catch (Exception)
			{
				// ErrorMessage 
			}

		}
	}
}
